package com.zdp.redisboot.controller;

import com.zdp.redisboot.cache.RedisManager;
import com.zdp.redisboot.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sesshomaru
 * @date 2019/11/30 17:34
 */
@RestController
@RequestMapping("/cluster")
public class ClusterController {

    @Autowired
    private RedisManager redisManager;

    @RequestMapping("/save")
    public Map save(){
        User user = new User();
        user.setBalance(BigDecimal.ONE);
        user.setBirthdayDate(LocalDateTime.now());
        user.setUserAge(18);
        user.setUserName("小明");
        user.setUserPhone("15871714****");
        user.setUserSex(1);
        Map<String,Object> result = new HashMap<>();
        try {
            redisManager.set("user1",user);
            result.put("type","success");
        }catch (Exception e){
            result.put("type","error");
        }
        return result;
    }

    @PostMapping("get")
    public Map get(HttpServletRequest request){
        String key = request.getHeader("key");
        User user = (User) redisManager.get(key);
        Map<String,Object> result = new HashMap<>();
        result.put("result",user);
        return result;
    }
}
